<?php 

/**
* 分类模型
*/
class CategoryModel extends Model{
	// 设置模型对象
	public $table="category";
	/**
	 * [get_data 获取所有分类]
	 * @return [type] [description]
	 */
	public function get_data(){
		return $this->all();
	}
	/**
	 * [add_data 增加分类的方法]
	 */
	public function add_data(){
		return $this->add();
	}
	/**
	 * [get_one_data 获取父级数据]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function get_one_data($where = NULL){
		return $this->where($where)->find();
	}
	/**
	 * [del_data 删除数据]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function del_data($cid){
		$data = $this->where(array('pid'=>$cid))->getField('cid');
		if($data){
			$this->error = '请先删除该分类的子级';
			return false;
		}
		$this->where(array('cid'=>$cid))->delete(); 
		return true;
	}
	/**
	 * [update_data 更新分类]
	 * @return [type] [description]
	 */
	public function update_data($where){
		$this->where($where)->save();
	}
	/**
	 * [change_stat 异步]
	 * @return [type] [description]
	 */
	public function change_stat(){
		//更改状态
		$stat = Q('post.stat', 0, 'intval');
		$cid = Q('post.cid', 0, 'intval');
		$type = Q('post.type');

		$where = array('cid'=>$cid);

		switch ($type) {
			case 'show':
				//如果$stat是0的时候，要关闭
				if($stat){
					$this->where($where)->update(array('is_show'=>0));
				}else{
					$this->where($where)->update(array('is_show'=>1));
				}
				break;
			case 'list':
				//如果$stat是0的时候，要关闭
				if($stat){
					$this->where($where)->update(array('is_listhtml'=>0));
				}else{
					$this->where($where)->update(array('is_listhtml'=>1));
				}
				break;
			case 'arc':
				//如果$stat是0的时候，要关闭
				if($stat){
					$this->where($where)->update(array('is_archtml'=>0));
				}else{
					$this->where($where)->update(array('is_archtml'=>1));
				}
				break;
			
			default:
				# code...
				break;
		}

		// $this->where($where)->update($update);

	}
	public function del_alldata(){
		$cid=Q("post.",0,"intval");
		$cid=current($cid);
		foreach ($cid as $v) {
			$this->where(array('cid'=>$v))->delete();
		}
		return true;
	}
	public function get_son_data($data,$cid){
		$arr = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$arr[] = $v['cid'];
				$arr = array_merge($arr,$this->get_son_data($data,$v['cid']));
			} 
		}
		return $arr;
	}
}

 ?>